--[[=========================================================
	OBJECT TYPE
=========================================================]]--

--[[-----------------------------------------------------]]--
--	Constructor
--[[-----------------------------------------------------]]--
ObjObjectType					= class( function( self, id, name, display, className ) 

	--	Members
	self.TableName				= "ObjectType"
	self.ID						= ValOrDef( id, "integer", 0 )
	self.Name					= ValOrDef( name, "string", "" )
	self.Display				= ValOrDef( display, "string", "" )
	self.ClassName				= ValOrDef( className, "string", "" )
	
--	SQL management - SERVER only
if SERVER then
	
	--	SQL Insert ( table, ids, contents )
	self.Insert					= function( self )
	
		--	Variables
		local result			= nil
	
		--	Generate a new ID
		if self.ID == 0 then
			self.ID					=
				sql.GetNewID(
					self.TableName,
					FieldInfo( "ID", self.ID, "number" )
				)
		end
		
		--	Insert in DB if we got an ID
		if self.ID != 0 then
			result = sql.Insert(
				self.TableName,
				{ 1 },
				FieldInfo( "ID", self.ID, "number" ),
				FieldInfo( "Name", self.Name, "string" ),
				FieldInfo( "Display", self.Display, "string" ),
				FieldInfo( "ClassName", self.ClassName, "string" )
			)
		end
	end
	
	--	SQL Update ( table, ids, contents )
	self.Update					= function( self )
	
		--	Variables
		local result			= nil
	
		--	Update in DB
		result = sql.Update(
			self.TableName,
			{ 1 },
			FieldInfo( "ID", self.ID, "number" ),
			FieldInfo( "Name", self.Name, "string" ),
			FieldInfo( "Display", self.Display, "string" ),
			FieldInfo( "ClassName", self.ClassName, "string" )
		)
	end
	
	--	SQL Delete ( table, ids, id contents )
	self.Delete					= function( self )
	
		--	Variables
		local result			= nil
	
		--	Delete from DB
		result = sql.Delete(
			self.TableName,
			{ 1 },
			FieldInfo( "ID", self.ID, "number" )
		)
		
		--	Reset ID if it was deleted
		if result then
			self.ID					= 0
		end
	end
	
	--	SQL Reload ( table, ids, id contents )
	self.Reload					= function( self, id )
	
		--	Redefinie id if needed
		if IsInt( id ) then
			self.ID				= id
		end
	
		--	Retrieve from DB
		self:FillProperties(
			sql.GetRow(
				self.TableName,
				{ 1 },
				FieldInfo( "ID", self.ID, "number" ),
				FieldInfo( "Name", self.Name, "string" ),
				FieldInfo( "Display", self.Display, "string" ),
				FieldInfo( "ClassName", self.ClassName, "string" )
			)
		)
	end
	
	--	Assign fields from row
	self.FillProperties			= function( self, row )
		self.Name				= tostring( row["Name"] )
		self.Display			= tostring( row["Display"] )
		self.ClassName			= tostring( row["ClassName"] )
	end
	
end	--	End of SQL management

--	Other server functions
if SERVER then

	--	Returns a table for transfers to clients
	self.GetClientTable				= function( self )
		return {
			["ID"]					= self.ID,
			["Name"]				= self.Name,
			["Display"]				= self.Display,
			["ClassName"]			= self.ClassName
		}
	end

end

end)